<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/15 0015
 * Time: 20:31
 */

namespace app\api\controller;


use app\lib\im\Tls;
use think\Config;
use think\Session;
use think\Validate;

class TlsApi
{
    private $identifier="";
    private $sdkappid="";
    private $usersig="";
    private $url="";

    public function __construct(){
        $this->identifier=Config::get("webim.identifier");
        $this->sdkappid=Config::get("webim.sdkappid");
        $this->usersig=(new Tls())->getUserSig($this->identifier);
        $rand=rand(10000,99999);
        $this->url="https://console.tim.qq.com/%s?usersig={$this->usersig}&identifier={$this->identifier}&sdkappid={$this->sdkappid}&random={$rand}&contenttype=json";
    }

    public function getUserSig(){
        $identifier=Session::get("mobile");
        $usersig=(new Tls())->getUserSig($identifier);
        return json([
            "code"          =>0,
            "mobile"        =>$identifier,
            "usersig"       =>$usersig
        ]);
    }

    //获取所有群组的ID
    public function getAllGroupID(){
        $url=sprintf($this->url,"v4/group_open_http_svc/get_appid_group_list");
        $data=request()->param();
        $resp=curl_post_request($url,[
            "Limit"     => $data['limit']?$data['limit']:100,
            "Next"      => $data["next"]?$data['next']:0,
            "GroupType" =>$data["groupType"]?$data['groupType']:"" // 拉取哪种群组形态，不填为拉取所有
        ]);
        if($resp){
            return json($resp,200);
        }
    }

    public function addGroupMember(){
        $url=sprintf($this->url,"v4/group_open_http_svc/add_group_member");
        $data=request()->param();
        $vali=Validate::make([
            "groupId"   =>"require",
            "memberAccount" =>"require"
        ])->batch();
        if(!$vali->check($data)){
            return json([
                "result"    =>-1,
                "errmsg"       =>$vali->getError()
            ]);
        }
        $resp=curl_post_request($url,[
           "GroupId"    =>$data['groupId'],
            "MemberList"   =>[
                [
                    "Member_Account"    =>$data['memberAccount']
                ]
            ]
        ]);
        if($resp){
            return json($resp,200);
        }
    }

    //获取群成员详细信息
    public function getGroupMemberInfo(){
        $url=sprintf($this->url,"v4/group_open_http_svc/get_group_member_info");
        $data=request()->param();
        $resp=curl_post_request($url,[
            "GroupId"   =>$data["groupId"]?$data["groupId"]:"",
            "Limit"     =>$data['limit']?$data["limit"]:100,
            "OffSet"    =>$data['offset']?$data["offset"]:0
        ]);
        if($resp){
            return json($resp,200);
        }
    }
}